import random


def retnum(times=1, exceptions=None):
	exceptions = exceptions if exceptions is not None else Exception
	
	def wrapper(func):
		def wrapper(*args, **kwargs):
			last_exception = None
			for _ in range(times):
				try:
					return func(*args, **kwargs)
				except exceptions as e:
					last_exception = e
			raise last_exception
		
		return wrapper
	
	return wrapper
	
	#
	# if __name__ == "__main__":
	# 	@retry(5)
	# 	def do_something_unreliable():
	# 		a = random.randint(0, 10)
	# 		print a
	# 		if a > 3:
	# 			raise IOError("Broken ")
	# 		else:
	# 			return "Awesome sauce!"
	#
	#
	# 	print do_something_unreliable()
